import { useUserStore } from '@/store/index'
const useUser = useUserStore()
// 检查用户权限
const checkPermission = (el: any, binding: any) => {
  const { value }: any = binding
  // 获取权限列表
  const roles = useUser.getRoles() || []
  if (value && value instanceof Array) {
    if (value.length > 0) {
      const elRoles = value
      const hasPermission = roles.some((role: any) => {
        return elRoles.includes(role)
      })
      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    }
  } else {
    throw new Error(`need a array like ['role1', 'role2']`)
  }
}

export default {
  mounted(el: any, binding: any) {
    checkPermission(el, binding)
  },
  updated(el: any, binding: any) {
    checkPermission(el, binding)
  }
}
